<html>

<head>
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
 
 <title>Nguyễn Quốc Thông - C12TDT </title>   
 
 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<link rel="stylesheet" type="text/css" href="public/CSS/css.css"> 
 


<h1> Nguyễn Quốc Thông</h1>
</head>
<body>
    <?php 
    require_once 'model/db2.php';?>
<?php 
    $query = "SELECT * FROM companies";
    $companies = getData($query);
    $query = "SELECT * FROM apps";
    $apps = getData($query);
?>
       

     
<form method="POST">
    Min fans<input type="text" name="min_fans" id="min_fans"style="border-color: #1480c8"border="1px"><br>
    Max fans<input type="text" name="max_fans" id="max_fans"style="border-color: #1480c8"border="1px"><br>
    Từ Khóa <input type="text" name="keyword"style="border-color: #1480c8"border="1px"><br>
    Công Ty<select name="id_company" style="border-color: #1480c8">
        <option value="0">Chọn Công Ty</option>
        <?php foreach ($companies as $company): ?>
        <option value="<?php echo $company['id'] ?>"><?php echo $company['name']?></option>
        <?php        endforeach;?>
    </select></br>
    
     Ứng Dụng :<select name="id_apps" style="border-color: #1480c8">
         <option value="0">Chọn Ứng Dụng</option>
        <?php foreach ($apps as $app): ?>
        <option value="<?php echo $app['id'] ?>"><?php echo $app['name']?>
        </option>
        <?php        endforeach;?>
    </select></br>
     
<input type="submit" name="submit" value="Tìm Kiếm">
<input type="button" name="check" id="check" value="Kiểm Tra">
</form>

<?php if (isset($_POST['submit'])): ?>
    <?php
    $min_fans = $_POST['min_fans']; //TODO
    $max_fans = $_POST['max_fans']; //TODO
    $keyword = $_POST['keyword']; //TODO
    $company = $_POST["id_company"];
    $apps = $_POST["id_apps"];
    if (!empty($company))
        $company = "(companies.id=$company) AND";
    else
        $company = "";
    if (!empty($apps))
        $apps = "(apps.id='$apps') AND";
    else
        $apps = "";



    if (empty($max_fans) && !empty($min_fans)) {
        $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps 
                  (pages.likes>= $min_fans) and
                  pages.description LIKE '% $keyword %'
                  LIMIT 0,20";
    } else if (!empty($max_fans) && empty($min_fans)) {
        $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                  (pages.likes<= $max_fans) and
                  pages.description LIKE '% $keyword %'
                  LIMIT 0,20";
    } else if (empty($max_fans) && empty($min_fans)) {
        $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                  pages.description LIKE '% $keyword %'
                  LIMIT 0,20";
    } else {
        $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                  (pages.likes>= $min_fans) and
                  (pages.likes<= $max_fans) and
                  pages.description LIKE '% $keyword %'
                  LIMIT 0,20";
    }


    var_dump($query);

    $pages = getData($query);
    ?>
    
    <?php if (!empty($pages)):?>
   <table id="table"style="border-color: #1480c8">
        <tr class=" khung">
            <td> Tên </td>
            <td> Ảnh đại diện </td>
             <td> Lượt Thích </td>
              <td> Công ty </td>
                <td> SĐT </td>
               <td> Giới thiệu </td>
                  <td> Trang Cá Nhân</td>
               <td> Trang Web </td>
                <td> Quốc Gia</td>
        </tr>
        <?php foreach ($pages as $page):?>
            <tr class=" khung1">>
                 <td><?php echo $page['username'] ?></td>
                <td><img src="<?php echo $page['picture']?>"></td>
                <td><?php echo $page['likes'] ?></td>
                <td><?php echo $page['name'] ?></td>
                <td><?php echo $page['phone'] ?></td>
                <td><?php echo substr($page["description"], 0,100); ?></td>
                <td><a href='<?php echo $page["link"] ?>'><?php echo $page["link"] ?></td>
                 <td><a href='<?php echo $page["website"] ?>'><?php echo $page["website"] ?></td>
                <td><?php echo $page['country'] ?></td>
                <?php // var_dump($page);die();?>
            </tr>
        <?php endforeach; ?>
    </table> 

    <?php endif;?>
<?php endif;?>
<script>
            $(document).ready(function() {
                $("#check").click(function() {
                    var min_fans = $("#min_fans").val();

                    //kiem tra so
                    if ($.isNumeric(min_fans)) {
                        alert('Min Is Number');
                    } else {
                        alert('Xin Nhập lại Số Min ?');
                    }
                });


                $("#check").click(function() {
                    var max_fans = $("#max_fans").val();


                    if ($.isNumeric(max_fans)) {
                        alert('Max Is Number');
                    } else {
                        alert('Xin Nhập lại Số Max ?');
                    }
                });
            });
</script>
</body>
</html>